var md5 = require('./md5.js');
var temp = null;
var loginback = null;

window.onload = function(){
	if(getdom_id("info")){
		getdom_id("info").onsubmit = login;
	}
    
	var data2 = JSON.parse(localStorage.getItem('info'));
	if(data2){
	  console.log("data2 ",data2);
	}
}

function getdom_id(name){
	return  document.getElementById(name);
}

function requestBack(url,_data,successback,errorback){
	layer.closeAll('loading');
	if(temp){
		clearTimeout(temp);
	}
	layer.load(2);
	//此处演示关闭
	temp = setTimeout(function(){
	  layer.closeAll('loading');
	  layer.msg('连接超时');
	}, 1000*20);
	
	$.ajax({
		type:"post",
		url:url,
		data:_data,
		async:true,
		success: function(data) {
			layer.closeAll('loading');
			if(temp){
				clearTimeout(temp);
			}
			successback(data);
        },
        error: function(request) {
          layer.closeAll('loading');
          if(temp){
			clearTimeout(temp);
		  }
          errorback(request);
        },
        fail: function(request) {
          layer.closeAll('loading');
          if(temp){
			clearTimeout(temp);
		  }
          errorback(request);
        }
	});
};

function login(){
	var _login = {name:getdom_id('name').value,pwd:md5(getdom_id('pwd').value),checkcode:getdom_id('checkcode').value};
	console.log(JSON.stringify(_login));
	
   localStorage.setItem('info',JSON.stringify(_login));
	
	requestBack(
		"http://118.193.50.90:5555/Game?a=login",
		JSON.stringify(_login),
		function(ret){
			if(ret.status==0){
				layer.msg(ret.info);
			}else if(ret.status==1){
				layer.msg(ret.info);
				loginback = ret;
			}
			console.log('success',loginback);
		},
		function(ret){
			layer.msg('连接出错啦!');
			console.log('error',ret);
		}
	);
	event.preventDefault();
};


